function useShowToast(message : string, icon : any = 'none', mask : boolean = true, position : any = "bottom") : void {
	uni.showToast({
		mask,
		icon: icon,
		position: position,
		title: message
	});
}



function uploadImage(filePath : string) : Promise<any> {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: 'http://gt.yftq.jchorn.com/api/common/upload',
			filePath: filePath,
			name: 'file',
			header: {
				token: uni.getStorageSync('token')
			},
			success: (uploadFileRes) => {
				return resolve(JSON.parse(uploadFileRes.data))
			},
			fail: (error) => {
				return reject(error)
			}
		});
	})
}

/**
 * 将UUID转换为MAC地址
 * @param {string} uuid 
 * @return 
 */
function uuid2mac(uuid : string) : string {
	// 使用正则表达式确保UUID格式正确
	const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
	if (!uuidPattern.test(uuid)) {
		throw new Error('Invalid UUID format');
	}

	// 移除UUID中的短横线
	const cleanUuid = uuid.replace(/-/g, '');

	// 取UUID的部分内容来模拟生成MAC地址格式
	// 注意：这只是一个演示，实际应用中应根据需求设计合理的映射逻辑
	const mac = cleanUuid.substring(0, 12);

	// 分组并插入冒号
	const formattedMac = mac.match(/.{2}/g)?.join(':') ?? '';

	return formattedMac;
}



// ArrayBuffer转16进度字符串示例
function ArrayBuffer2hex(buffer : Array<any>) : string {
	const hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),
		function (bit : number) {
			return ('00' + bit.toString(16)).slice(-2)
		}
	)
	return hexArr.join('')
}

// 16进制字符串转ArrayBuffer
function hex2ArrayBuffer(hexString : string) : Uint8Array {
	// 移除可能的前导 "0x"
	hexString = hexString.replace(/^0x/i, '');
	// 确保字符串长度是偶数，如果不是则前置补0
	if (hexString.length % 2 !== 0) {
		hexString = '0' + hexString;
	}
	// 将每两个字符一组转换为其对应的字节，并组合成Uint8Array
	const byteArray = new Uint8Array(hexString.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) ?? []);

	return byteArray;
}

type ParseHexDataResponse = {
	type : string,
	length : number,
	data : {
		[key : string] : any
	}
}

function parseHexData(hexString, keys) : ParseHexDataResponse {
	// 去除可能的空格并确保字符串长度是偶数，以便正确配对
	hexString = hexString.replace(/\s+/g, '');
	if (hexString.length % 2 !== 0) {
		throw new Error("Input string length must be even to represent hexadecimal pairs correctly.");
	}

	// 转换为数字数组
	const hexArray = [];
	for (let i = 0; i < hexString.length; i += 2) {
		hexArray.push(parseInt(hexString.substring(i, i + 2), 16));
	}

	// 提取交互类型和数据长度
	const type = hexArray[0].toString();
	const length = hexArray.slice(1, 3).reduce((acc, val) => acc * 256 + val, 0);

	// 确保数据长度与keys数组长度匹配
	if (length / 2 !== keys.length) {
		throw new Error("Length of 'keys' array does not match the data length indicated in the input.");
	}

	// 解析数据对
	const data = {};
	for (let i = 0; i < length / 2; i++) {
		const keyHex = keys[i];
		const valueHex = hexArray.slice(3 + i * 2, 3 + i * 2 + 2);
		data[keyHex] = valueHex.reduce((acc, val) => acc * 256 + val, 0);
	}

	return { type, length, data };
}

function decimalToHex(decimal : number, includePrefix : boolean = false) : string {
	// toString(16)将数字转换为16进制字符串
	// 前缀'0x'是为了符合某些场景下的显示习惯，可按需调整
	// padStart(2, '0')确保至少有两位，不足则用0填充
	let hexStr = decimal.toString(16).toUpperCase().padStart(2, '0');
	if (includePrefix) {
		hexStr = '0x' + hexStr;
	}
	return hexStr;
}

export { hex2ArrayBuffer, ArrayBuffer2hex, decimalToHex, parseHexData, uuid2mac, useShowToast, uploadImage }